const compiler = require('vue-template-compiler')
const path = require('path')

module.exports = {
  title: 'mengyuan-ui',
  lang: 'zh-CN',
  description: '使用 element-ui 二次封装组件',
  themeConfig: {
    nav: [
      { text: '首页', link: '/' },
      { text: '列子', link: '/example/左树右表' },
      { text: '指南', link: '/guide/design' },
      { text: '组件', link: '/components/button' }
    ],
    sidebar: {
      '/example/': [
        {
          title: '例子',
          collapsable: false,
          children: ['左树右表']
        }
      ],
      '/guide/': ['design', 'start'],
      '/components/': [
        {
          title: '基础组件',
          collapsable: false,
          children: ['layout', 'button', 'form', 'table', 'tree', 'dialog']
        }
      ]
    },
    sidebarDepth: 2
  },
  plugins: [
    [
      'vuepress-plugin-container',
      {
        type: 'demo',
        render: function (tokens, idx) {
          var m = tokens[idx]

          if (tokens[idx].nesting === 1) {
            const info = encodeURI(tokens[idx].info.trim().replace(/^demo/, '').trim())
            const { content } = tokens[idx + 1]
            const { script, template } = compiler.parseComponent(content)
            const renderStr = encodeURI(compiler.compileToFunctions(template.content).render.toString())
            const scriptStr = encodeURI(script.content.replace('export default', '').trim())
            return `<m-demo info="${info}" script="${scriptStr}" render="${renderStr}">`
          } else {
            return '</m-demo>'
          }
        }
      }
    ]
  ],
  configureWebpack: {
    node: {
      global: true,
      process: true
    },
    resolve: {
      alias: {
        // vuepress 依赖 core-js 2.x，element-ui 依赖 core-js 3.x，安装是 3.x，得改一下目录别名
        'core-js/library/fn': 'core-js/features',
        '@utils': path.resolve(__dirname, '../..', 'src/utils')
      }
    }
  }
}
